// pages/bus/line/line.js
var config = wx.getStorageSync('config');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    name: "",
    dir: 1,
    line: null, // 线路相关的所有信息，包括起始站，发车事件，经过的站点
    planTime: null,
    nowStationId: 0, // 用户当前选中的站点id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      "id": options.id,
      "name":options.name,
      "nowStationId":0,

      // 测试数据
      // "id": 1231,
      // "name": "123",
      // "nowStationId": 46852273,
    })
    console.log(options.id)
  },


  changeDirection: function () {

    // 先停止定时器
    clearInterval(this.data.nowBusStationTimer)
    clearInterval(this.data.getPlanRunTimeTimer)

    let dirs = this.data.line.directions;
    var dir = this.data.dir;
    if (!dirs || dirs.length <= 1) {
      return;
    }
    for (var i = 0; i < dirs.length; i++) {
      if (dir == dirs[i]) {
        // 如果是最后一个，就设置为第一个方向
        if (i == dirs.length - 1) {
          dir = dirs[0];
        } else { // 否则就是下一个
          dir = dirs[i + 1]
        }
        break;
      }
    }
    this.setData({
      "dir": dir
    })
    this.getLine();
    this.nowBusStation()
    this.getPlanRunTime();
  },
  nowBusStation: function () { // 查看指定线路中到指定站还有几站
    var that = this;
    // 默认使用最后一个最为目标
    if (this.data.nowStationId == 0) {
      this.setData({"nowStationId":this.data.line.stations[0].id})
    }
    wx.request({
      url: config.host + 'mini/bus/nowBusStation/' + this.data.id + "/" + this.data.nowStationId + "/" + this.data.dir,
      success: function (res) {
        if (res.statusCode != 200) {
          console.log(res)
          return;
        }
        var data = res.data;
        console.log(data);
        if (data.simpleDistances) {
          for (var i = 0; i < data.simpleDistances.length; i++) {
            if (data.simpleDistances[i].stopNbr < 0) {
              data.simpleDistances[i].stopNbr = 0;
            }
          }
        }
        that.setData({
          "simple": data.simpleDistances
        })

        if (data.nowBuses && that.data.line.stations) {

          var line = that.data.line;
          
          for (var i = 0; i < line.stations.length; i++) {
            line.stations[i].here = 0; // 清除上次的记录，防止车经过的站都被设置为有车
            data.nowBuses.forEach(e => {
              if (e.id == line.stations[i].dualserialId) {
                line.stations[i].here = 1;
              }
            });
          }
          that.setData({
            "line": line
          })
          that.getSimpleViewHeight();

        }



      },
      fail: function (err) {
        console.log(err)
      }
    })
  },
  getLine: function () {

    var that = this;
    wx.request({
      url: config.host + 'mini/bus/queryBus/' + this.data.id + "/" + this.data.dir,
      success: function (res) {
        if (res.statusCode != 200) {
          console.log(res)
          return;
        }
        var data = res.data;
        console.log(data);
        that.setData({
          "line": data
        })

        that.nowBusStation()
        that.data.nowBusStationTimer = setInterval(() => {
          that.nowBusStation()
        }, 20000);

        that.getPlanRunTime()
        that.data.getPlanRunTimeTimer = setInterval(() => {
          that.getPlanRunTime()
        }, 60000);

        // 更新高度
        that.getSimpleViewHeight();
      },
      fail: function (err) {
        console.log(err)
      }
    })
  },

  getPlanRunTime: function () {
    var that = this;
    wx.request({
      url: config.host + 'mini/bus/planRunTime/' + this.data.id + "/" + this.data.dir,
      success: function (res) {
        if (res.statusCode != 200) {
          console.log(res)
          return;
        }
        var data = res.data;
        console.log(data);
        that.setData({
          "nextTime": data.nextTime
        })

      },
      fail: function (err) {
        console.log(err)
      }
    })
  },

  setStation: function (e) {
    var param = e.currentTarget.dataset;
    console.log(param.id)
    this.setData({
      "nowStationId": param.id
    })
    this.nowBusStation()
  },

  getSimpleViewHeight: function () {
    var that = this;
    wx.createSelectorQuery().select('.simple-info-box').boundingClientRect(function (rect) {
      rect.id // 节点的ID
      rect.dataset // 节点的dataset
      rect.left // 节点的左边界坐标
      rect.right // 节点的右边界坐标
      rect.top // 节点的上边界坐标
      rect.bottom // 节点的下边界坐标
      rect.width // 节点的宽度
      rect.height // 节点的高度
      that.setData({
        "simpeInfoViewHeight": rect.height
      })
    }).exec()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getLine()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.nowBusStationTimer)
    clearInterval(this.data.getPlanRunTimeTimer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.nowBusStationTimer)
    clearInterval(this.data.getPlanRunTimeTimer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})